package com.dlc.shop.live.platform.controller;

import cn.hutool.core.util.PhoneUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dlc.shop.bean.model.User;
import com.dlc.shop.common.config.Constant;
import com.dlc.shop.common.exception.YamiShopBindException;
import com.dlc.shop.common.response.ServerResponseEntity;
import com.dlc.shop.common.util.PageParam;
import com.dlc.shop.common.util.PrincipalUtil;
import com.dlc.shop.live.common.model.LiveRoom;
import com.dlc.shop.live.common.service.LiveRoomProdService;
import com.dlc.shop.live.common.service.LiveRoomService;
import com.dlc.shop.live.common.vo.LiveRoomProdVO;
import com.dlc.shop.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @author lhd
 * @date 2020-08-05 08:53:17
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/live/liveRoom")
@Tag(name = "平台端直播间接口")
public class LiveRoomController {

    private final LiveRoomService liveRoomService;
    private final UserService userService;
    private final LiveRoomProdService liveRoomProdService;

    @GetMapping("/page")
    @Operation(summary = "分页查找直播间信息")
    @PreAuthorize("@pms.hasPermission('platform:liveRoom:page')")
    public ServerResponseEntity<IPage<LiveRoom>> getLiveRoomPage(PageParam<LiveRoom> page,  LiveRoom liveRoom) {
        IPage<LiveRoom> roomPage = liveRoomService.pageLiveRoom(page, liveRoom);
        for (LiveRoom record : roomPage.getRecords()) {
            if (Objects.nonNull(record.getUserMobile())) {
                record.setUserMobile(PhoneUtil.hideBetween(record.getUserMobile()).toString());
            }
            //昵称符合正则时就**
            if (PrincipalUtil.isMobile(record.getNickName())) {
                record.setNickName(PhoneUtil.hideBetween(record.getNickName()).toString());
            }
        }
        return ServerResponseEntity.success(roomPage);
    }

    @GetMapping("/info/{roomId}")
    @Operation(summary = "通过id查询直播间")
    @Parameter(name = "id", description = "直播间id", required = true)
    @PreAuthorize("@pms.hasPermission('platform:liveRoom:info')")
    public ServerResponseEntity<LiveRoom> getById(@PathVariable("roomId") Long roomId) {
        LiveRoom liveRoomDb = liveRoomService.getById(roomId);
        if (Objects.isNull(liveRoomDb)) {
            // 直播间不存在，请刷新后重试
            throw new YamiShopBindException("yami.live.not.exist");
        }
        User user = userService.getUserByUserId(liveRoomDb.getUserId());
        if (Objects.isNull(user)) {
            // 该主播已经注销账号
            liveRoomDb.setNickName("*用户已注销*");
            liveRoomDb.setUserMobile("*用户已注销*");
            liveRoomDb.setStatus(-1);
        } else {
            liveRoomDb.setNickName(user.getNickName());
            liveRoomDb.setUserMobile(user.getUserMobile());
            liveRoomDb.setStatus(user.getStatus());
        }
        return ServerResponseEntity.success(liveRoomDb);
    }

    @GetMapping("/listLiveRoomProd")
    @Operation(summary = "获取直播间的直播商品列表")
    @PreAuthorize("@pms.hasPermission('platform:liveRoom:listProd')")
    public ServerResponseEntity<List<LiveRoomProdVO>> listLiveRoomProd(@RequestParam Long roomId) {
        return ServerResponseEntity.success(liveRoomProdService.listLiveRoomProdInfo(roomId));
    }

    @PutMapping("/toTop/{id}")
    @PreAuthorize("@pms.hasPermission('platform:liveRoom:update')")
    @Operation(summary = "通过id置顶直播间")
    @Parameter(name = "id", description = "直播间id", required = true)
    public ServerResponseEntity<Boolean> toTopRoom(@PathVariable Long id) {
        LiveRoom liveRoom = liveRoomService.getById(id);
        if (Objects.isNull(liveRoom)) {
            // 直播间不存在，请刷新后重试
            throw new YamiShopBindException("yami.live.not.exist");
        }
        liveRoom.setRoomTop(Objects.equals(liveRoom.getRoomTop(), 0) ? 1 : 0);
        return ServerResponseEntity.success(liveRoomService.updateById(liveRoom));
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('platform:liveRoom:delete')")
    @Operation(summary = "通过id删除直播间商品库")
    @Parameter(name = "id", description = "直播间id", required = true)
    public ServerResponseEntity<Boolean> removeById(@PathVariable Long id) {
        return ServerResponseEntity.success(liveRoomService.removeRoomById(id, Constant.PLATFORM_SHOP_ID));
    }

    @PostMapping("/offline")
    @PreAuthorize("@pms.hasPermission('platform:liveRoom:offline')")
    @Operation(summary = "下线直播间")
    public ServerResponseEntity<Void> offline(@RequestBody LiveRoom liveRoom) {
        liveRoomService.offline(liveRoom);
        return ServerResponseEntity.success();
    }
}
